package com.yunti.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunti.business.domain.BaseSchool;
import com.yunti.business.mapper.BaseSchoolMapper;
import com.yunti.business.service.IBaseSchoolService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yunti.business.domain.BaseStudent;
import com.yunti.business.service.IBaseStudentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生Controller
 *
 * @author ruoyi
 * @date 2024-04-30
 */
@RestController
@RequestMapping("/business/student")
public class BaseStudentController extends BaseController
{
    @Autowired
    private IBaseStudentService baseStudentService;

    @Autowired
    private BaseSchoolMapper baseSchoolMapper;

    /**
     * 查询学生列表
     */
    @PreAuthorize("@ss.hasPermi('business:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseStudent baseStudent)
    {
        startPage();
        List<BaseStudent> list = baseStudentService.selectBaseStudentList(baseStudent);
        return getDataTable(list);
    }

    @Log(title = "学生管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:school:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseStudent> util = new ExcelUtil<BaseStudent>(BaseStudent.class);
        List<BaseStudent> students = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = baseStudentService.importSstudent(students, updateSupport, operName);
        return success(message);
    }

    /**
     * 导出学生列表
     */
    @PreAuthorize("@ss.hasPermi('business:student:export')")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseStudent baseStudentReq)
    {
        List<BaseStudent> list = baseStudentService.selectBaseStudentList(baseStudentReq);
        ExcelUtil<BaseStudent> util = new ExcelUtil<BaseStudent>(BaseStudent.class);

        LambdaQueryWrapper<BaseSchool> baseSchoolLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseSchoolLambdaQueryWrapper.in(BaseSchool::getId,list.stream().map(BaseStudent::getSchoolId).collect(Collectors.toList()));
        List<BaseSchool> baseSchoolList = baseSchoolMapper.selectList(baseSchoolLambdaQueryWrapper);

        Map<Long,BaseSchool> baseSchoolMap = baseSchoolList.stream().collect(Collectors.toMap(BaseSchool::getId, e1->e1));
        for (int i = 0; i < list.size(); i++) {
            BaseStudent baseStudent = list.get(i);
            if(baseSchoolMap.get(baseStudent.getSchoolId())!=null){
                baseStudent.setSchoolName(baseSchoolMap.get(baseStudent.getSchoolId()).getName());
            }

        }
        util.exportExcel(response, list, "学生数据");
    }

    /**
     * 导出确认表
     */
    @PreAuthorize("@ss.hasPermi('business:student:export')")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/exportConfirm")
    public void exportConfirm(HttpServletResponse response, BaseStudent baseStudent)
    {
        baseStudentService.exportConfirm(baseStudent,response);
    }

    /**
     * 获取学生详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseStudentService.selectBaseStudentById(id));
    }

    /**
     * 新增学生
     */
    @PreAuthorize("@ss.hasPermi('business:student:add')")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseStudent baseStudent)
    {
        return toAjax(baseStudentService.insertBaseStudent(baseStudent));
    }

    /**
     * 修改学生
     */
    @PreAuthorize("@ss.hasPermi('business:student:edit')")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseStudent baseStudent)
    {
        return toAjax(baseStudentService.updateBaseStudent(baseStudent));
    }

    /**
     * 删除学生
     */
    @PreAuthorize("@ss.hasPermi('business:student:remove')")
    @Log(title = "学生", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseStudentService.deleteBaseStudentByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BaseStudent> util = new ExcelUtil<BaseStudent>(BaseStudent.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @PostMapping("/confirmStudent")
    public AjaxResult confirmStudent(@RequestParam Long stuId, @RequestParam Integer mode)
    {
        return success(baseStudentService.confirmStudent(stuId,mode));
    }



    @PostMapping("/confirmStudentList")
    public AjaxResult confirmStudentList(@RequestParam ArrayList<Long> stuIdList, @RequestParam Integer mode)
    {
        return success(baseStudentService.confirmStudentList(stuIdList,mode));
    }

    /**
     * 查询学生列表
     */
    @PreAuthorize("@ss.hasPermi('business:student:list')")
    @GetMapping("/confirmList")
    public TableDataInfo confirmList(@RequestParam Long schoolId,@RequestParam Integer mode)
    {
        List<BaseStudent> list = baseStudentService.confirmList(schoolId,mode);
        return getDataTable(list);
    }
}
